package service

import (
	"fmt"
	"ksd-social-api/commons/base/service"
	context3 "ksd-social-api/modules/msg/model/context"
	cmodel "ksd-social-api/modules/note/model"
	"strconv"
)

type NoteCodeService struct {
	service.BaseService
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 查询小册和code是否存在
 */
func (service *NoteCodeService) DuihuanNote(code string, noteid uint64, userId uint64, systemId uint64, systemName string) bool {
	noteCode := noteCodeMapper.CountNoteCode(code, noteid, systemId)
	// 如果还没兑换，就开始兑换，生成小册订单，同时删除订单
	if noteCode > 0 {
		// 删除兑换的订单
		noteCodeMapper.UpdateNoteCode(code, noteid, systemId)
		// 开始保存订单
		service.saveUserNoteOrder(code, noteid, userId, systemId, systemName)
		// 兑换成功
		return true
	}
	return false
}

/**
 * 保存用户小册订单
 * @author feige
 * @date 2023-12-06
 * @version 1.0
 * @desc
 */
func (service *NoteCodeService) saveUserNoteOrder(code string, noteId uint64, userId uint64, systemId uint64, systemName string) {
	// 1： 判断用户是否购买此小册，如果购买过了就就不要去保存订单
	isBuyFlag := userBuyNoteMapper.CountUserBuyNoteNo(userId, noteId, systemId)
	if isBuyFlag == 0 {
		// 根据小册id查询小册信息
		note, _ := noteMapper.GetByIdTOPay(noteId, systemId)
		userVo := userMapper.GetUserByID(userId, systemId)

		userBuyNote := cmodel.UserBuyNote{}
		OutTradeNo := service.GetSnowWorkerIdString(8)
		// 处理返回结果
		userBuyNote.Opid = noteId
		userBuyNote.Title = note.Title
		userBuyNote.Cover = note.Img

		userBuyNote.UserId = userId
		userBuyNote.Username = userVo.Username
		userBuyNote.Avatar = userVo.Avatar
		userBuyNote.Uuid = userVo.Uuid
		userBuyNote.Phone = userVo.Telephone
		userBuyNote.Address = userVo.Address
		userBuyNote.Nickname = userVo.Nickname
		userBuyNote.SpecialId = note.SpecialId

		userBuyNote.ChannelId = note.ChannelId
		userBuyNote.ChannelName = note.ChannelName

		userBuyNote.Code = code
		userBuyNote.PayMethod = 3
		userBuyNote.PayMethodName = "兑换码"
		userBuyNote.Tradeno = OutTradeNo
		userBuyNote.Orderno = OutTradeNo
		userBuyNote.OrderJson = ""
		userBuyNote.SystemId = systemId
		userBuyNote.SystemName = systemName
		userBuyNote.Price = note.Price
		// 保存订单的接口
		userBuyNoteMapper.SaveUserBuyNote(userBuyNote)

		// 保存消息
		messageMeContext := context3.MessageMeContext{}
		messageMeContext.UserId = userId
		messageMeContext.Title = note.Title
		messageMeContext.Uuid = userVo.Uuid
		messageMeContext.SystemId = systemId
		messageMeContext.SystemName = "系统512"
		messageMeContext.Username = userVo.Nickname
		if note.ChapterFlag == 1 {
			messageMeContext.Url = fmt.Sprintf("https://www.study512.com/note/page/%d", note.Id)
		} else {
			messageMeContext.Url = fmt.Sprintf("https://www.study512.com/note/detail/%d", note.Id)
		}
		messageMeService.SaveMessageMeBuyNote(&messageMeContext)
	}
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 查询小册和code是否存在
 */
func (service *NoteCodeService) DuihuanCoinNote(noteId uint64, userId uint64, systemId uint64, systemName string) bool {
	// 根据小册id查询小册信息
	note, _ := noteMapper.GetByIdTOPay(noteId, systemId)
	userVo := userMapper.GetUserByID(userId, systemId)
	price, _ := strconv.Atoi(note.Price)
	if userVo.Coin <= 0 || price > userVo.Coin {
		return false
	}
	// 1： 判断用户是否购买此小册，如果购买过了就就不要去保存订单
	isBuyFlag := userBuyNoteMapper.CountUserBuyNoteNo(userId, noteId, systemId)
	if isBuyFlag == 0 {
		userBuyNote := cmodel.UserBuyNote{}
		OutTradeNo := service.GetSnowWorkerIdString(10)
		// 处理返回结果
		userBuyNote.Opid = noteId
		userBuyNote.Title = note.Title
		userBuyNote.Cover = note.Img
		userBuyNote.UserId = userId
		userBuyNote.Username = userVo.Username
		userBuyNote.Avatar = userVo.Avatar
		userBuyNote.Uuid = userVo.Uuid
		userBuyNote.Phone = userVo.Telephone
		userBuyNote.Address = userVo.Address
		userBuyNote.Nickname = userVo.Nickname

		userBuyNote.Code = ""
		userBuyNote.PayMethod = 4
		userBuyNote.PayMethodName = "学习币"
		userBuyNote.Tradeno = OutTradeNo
		userBuyNote.Orderno = OutTradeNo
		userBuyNote.OrderJson = ""
		userBuyNote.SystemId = systemId
		userBuyNote.SystemName = systemName
		userBuyNote.Price = note.Price
		// 保存订单的接口
		userBuyNoteMapper.SaveUserBuyNoteCoin(userBuyNote)
	}
	// 兑换成功
	return true
}
